package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Shopping;
import com.itheima.service.ShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingController {
    @Resource
    private ShoppingService shoppingService;

    /**
     * F05-购物车列表
     */
    @GetMapping("/list")
    public R all(){
        List<Shopping> all = shoppingService.all();
        return R.success(all);
    }

    /**
     * F04-加入购物车
     */
    @PostMapping("/add")
    public R add(@RequestBody Shopping shopping) throws InterruptedException {
        // synchronized (this){
            Shopping add = shoppingService.add(shopping);
            // Thread.sleep(300);
            if (add != null){
                return R.success(add);
            }
            return R.error("加入失败");
        // }
    }

    /**
     * F06-清空购物车
     */
    @DeleteMapping("/clean")
    public R delete(){
        Integer delete = shoppingService.delete();
        if (delete != 0){
            return R.success("清空购物车成功");
        }
        return R.error("清空购物车失败");
    }

    /**
     * F07-删除购物车商品
     */
    @PostMapping("/sub")
    public R newd(@RequestBody Shopping shopping){
        Integer newdelete = shoppingService.newdelete(shopping.getDishId(), shopping.getSetmealId());
        if (newdelete != 0){
            return R.success("");
        }
        return R.error("");
    }



}
